<template>
  <div class="topbar">
    <div class="wrap topbarwrap">
      <!-- 左边模块 -->
      <div class="left">欢迎来到叩丁狼积分商城</div>

      <!-- 右边模块 -->
      <ul>
        <li>
          <img :src="userInfo.headImg" alt="" width="23px" />
          用户名:{{ userInfo.nickName }}
        </li>
        <li>我的积分:{{ userInfo.coin }}</li>
        <li>获取积分</li>
        <li>叩丁狼官网</li>
        <!-- 购物车布局 -->
        <li class="cart-btn" v-if="isLogined">
          <img src="../assets/img/cart.png" alt="" />
          <span>购物车</span>
          <strong>{{ cartTotal }}</strong>
        </li>
        <!-- 点击登录显示显示框 -->
        <li class="current" v-else @click="changeIsShowLoginModal(true)">
          登录
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
import { mapMutations, mapState, mapActions } from "vuex";

//导入微信扫码登录api
import { WeixinLoginApi } from "@/request/api";
export default {
  computed: {
    ...mapState({
      isLogined: (state) => state.loginStatus.isLogined,
      cartTotal: (state) => state.userInfo.cartTotal,
      userInfo: (state) => state.userInfo.userInfo,
    }),
  },
  methods: {
    ...mapMutations({
      changeIsShowLoginModal: "ShowLogin/changeIsShowLoginModal",
      changeIsLogined: "loginStatus/changeIsLogined",
    }),
    ...mapActions({
      asyncChangeIsShowToast: "showToast/asyncChangeIsShowToast",
      asyncChangeUserInfo: "userInfo/asyncChangeUserInfo",
    }),
  },
  //两种场景会执行当前created()
  //1项目打开,页面刷新
  //2 微信扫描后,页面自动刷新 同时地址栏中携带一个code参数
  async created() {
    // 获取code值(当微信你用户点击允许后,页面重新刷新,也就会重新执行)
    let code = this.$route.query.code;
    if (code) {
      // 微信扫码登录
      //找后台去微信换取token
      let res = await WeixinLoginApi({
        code,
      });
      console.log("登录微信后res", res);

      if (res.code == 0) {
        //1 code ==0,登录成功
        this.asyncChangeIsShowToast({
          msg: "登录成功",
          type: "success",
        });
        // 2 保存token值
        localStorage.setItem("x-auth-token", res["x-auth-token"]);
        // 3 改变登录状态
        this.changeIsLogined(true);
        //4 清除地址栏中的code
        this.$route.query.code && this.$router.push(this.route.path);
        this.asyncChangeUserInfo();
      } else if (res.code == 400) {
        // code过期的情况
        // 提示失效
        this.asyncChangeIsShowToast({
          msg: "二维码已经失效,请重新扫描登录",
          type: "danger",
        });
        //把登录模态窗口打开
        this.changeIsShowLoginModal(true);
      } else if (res.coe == 407) {
        // 用户的微信还没有在这个网站上绑定过手机
        //提示绑定
        this.asyncChangeIsShowToast({
          msg: "请使用手机号码绑定微信登录",
          type: "waring",
        });
        this.changeIsShowLoginModal(true);

        localStorage.setItem("uuid", res.uuid);
      } else {
        //判断是否有toekn
        let token = localStorage.getItem("x-auth-token");
        this.changeIsLogined(Boolean(token));

        if (token) {
          this.asyncChangeUserInfo();
        } else {
          this.initUserInfo();
        }
      }
    }
  },

  // watch:{
  //   '$route.path':{
  //     handler(newVal,oldVal){
  //       let token = localStorage.getItem('x-auth-token');
  //       // if(token){
  //       //   this.changeIsLogined(true);
  //       // }else{
  //       //   this.changeIsLogined(false);
  //       // }
  //       this.changeIsLogined(Boolean(token))
  //     }
  //   }
  // }
};
</script>

<style lang='less' scoped>
.topbar {
  height: 40px;
  background: #242b39;
  color: #fff;
  .topbarwrap {
    display: flex;
    justify-content: space-between;
    line-height: 40px;
    font-size: 16px;
  }
  ul {
    text-align: center;
    display: flex;
    justify-content: space-between;
    li {
      margin-right: 20px;
      &.current {
        width: 124px;
        background: #0a328e;
      }
      .cart-btn {
        width: 124px;
        height: 40px;
        background: #0a328e;
        text-align: center;
        img {
          width: 20px;
          height: 19px;
          vertical-align: middle;
        }
        span {
          margin: 0 8px;
        }
        strong {
          width: 22px;
          height: 22px;
          line-height: 22px;
          display: inline-block;
          border-radius: 50%;
          background-color: #fd604d;
        }
      }
    }
  }
  img {
    vertical-align: middle;
    border-radius: 50%;
  }
}
</style>